package clients.producer;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * 生产者源码调试入口
 *
 * @author kerry dong
 */
public class KafkaProducerDebug {

	public static void main(String[] args) {
		Properties properties = new Properties();
		// 服务端地址
		properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
		// key序列化方式
		properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
		// value序列化方式
		properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
		// 创建producer
		KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
		for (int i = 0; i < 5; i++) {
			// 发送消息到服务器
			kafkaProducer.send(new ProducerRecord<>("fish", "value" + i), (metadata, exception) -> {
				// 回调通知
				int partition = metadata.partition();
				String topic = metadata.topic();
				System.out.println("msg send success :topic:" + topic + ",partition:" + partition);
			});
		}
		// 关闭资源
		kafkaProducer.close();
	}
}
